Public Class Connection
    Implements Data.IDbConnection

    Private _state As System.Data.ConnectionState
    Private _ConnectionString As String
    Private _credentials As System.Net.ICredentials

    Private props As Dictionary(Of String, String)

    Private _lists As lists.Lists
    Private _Views As Views.Views

    Friend Property Lists() As Lists.Lists
        Get
            Return _lists
        End Get
        Set(ByVal value As Lists.Lists)
            _lists = value
        End Set
    End Property
    Friend Property Views() As Views.Views
        Get
            Return _Views
        End Get
        Set(ByVal value As Views.Views)
            _Views = value
        End Set
    End Property

    Public Function BeginTransaction() As System.Data.IDbTransaction Implements System.Data.IDbConnection.BeginTransaction
        Return New Transaction()
    End Function

    Public Function BeginTransaction(ByVal il As System.Data.IsolationLevel) As System.Data.IDbTransaction Implements System.Data.IDbConnection.BeginTransaction
        Return New Transaction()
    End Function

    Public Sub ChangeDatabase(ByVal databaseName As String) Implements System.Data.IDbConnection.ChangeDatabase

    End Sub

    Public Sub Close() Implements System.Data.IDbConnection.Close
        _state = ConnectionState.Closed
    End Sub

    Public Property ConnectionString() As String Implements System.Data.IDbConnection.ConnectionString
        Get
            Return _ConnectionString
        End Get
        Set(ByVal value As String)
            props = GetProperties(value)
            _ConnectionString = value
        End Set
    End Property

    Private Function GetProperties(ByVal connectionString As String) As Dictionary(Of String, String)
        Dim dictionary As New Dictionary(Of String, String)
        If connectionString.Contains(";") = True Then
            For Each param As String In connectionString.Split(";")
                If param.Contains("=") = True Then
                    Dim nameValue As String() = param.Split("=")
                    dictionary.Add(nameValue(0).ToUpper(), nameValue(1))
                End If
            Next
        End If
        Return Dictionary
    End Function

    Public Property Credentials() As System.Net.ICredentials
        Get
            Return _lists.Credentials
        End Get
        Set(ByVal value As System.Net.ICredentials)
            _lists.Credentials = value
            _Views.Credentials = value
        End Set
    End Property

    Private _URL As String

    Public Property URL() As String
        Get
            Return _URL
        End Get
        Set(ByVal value As String)
            _URL = value
            Me.ListsURL = _URL & "lists.asmx"
            Me.ViewURL = _URL & "Views.asmx"
        End Set
    End Property

    Private Property ListsURL() As String
        Get
            Return _lists.Url
        End Get
        Set(ByVal value As String)
            _lists.Url = value
        End Set
    End Property
    Private Property ViewURL() As String
        Get
            Return _Views.Url
        End Get
        Set(ByVal value As String)
            _Views.Url = value
        End Set
    End Property

    Public ReadOnly Property ConnectionTimeout() As Integer Implements System.Data.IDbConnection.ConnectionTimeout
        Get

        End Get
    End Property

    Public Function CreateCommand() As System.Data.IDbCommand Implements System.Data.IDbConnection.CreateCommand
        Return New Command()
    End Function

    Public ReadOnly Property Database() As String Implements System.Data.IDbConnection.Database
        Get

        End Get
    End Property

    Public Sub Open() Implements System.Data.IDbConnection.Open
        _state = ConnectionState.Open
    End Sub

    Public ReadOnly Property State() As System.Data.ConnectionState Implements System.Data.IDbConnection.State
        Get
            Return _state
        End Get
    End Property

    Private disposedValue As Boolean = False        ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: free managed resources when explicitly called
            End If

            ' TODO: free shared unmanaged resources
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region
    Private Sub Initialize()
        If props.ContainsKey("URL") Then
            Me.URL = props.Item("URL")
        End If

        If props.ContainsKey("USERNAME") Then
            Dim userName As String = String.Empty
            Dim Password As String = String.Empty
            Dim domain As String = String.Empty

            userName = props.Item("USERNAME")
            If props.ContainsKey("PASSWORD") Then
                Password = props.Item("PASSWORD")
            End If
            If props.ContainsKey("DOMAIN") Then
                domain = props.Item("DOMAIN")
                Me.Credentials = New System.Net.NetworkCredential(userName, Password, domain)
            Else
                Me.Credentials = New System.Net.NetworkCredential(userName, Password)
            End If
        End If

    End Sub
    Public Sub New(ByVal connectionString As String)
        MyClass.New()
        Me.ConnectionString = connectionString
        Initialize()
    End Sub
    Public Sub New()
        _lists = New lists.Lists
        _lists.PreAuthenticate = True

        _Views = New Views.Views
        _Views.PreAuthenticate = True
        Me.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
    End Sub
End Class
